import IMusicInfo from "./IMusicInfo";
import AxiosIntance from "axios";

export default class MusicBox {
  /**
   * 播放器对象
   */
  private musicDom: HTMLAudioElement;

  
  private onPause?: () => any;
  private onPlaying?: (progress: number) => any;
  private onInit?: (ms: IMusicInfo[]) => any;
  
  /**
   * 播放索引
   */
  playIndex: number = 0;
  /**
   * 音乐曲
   */
  musics: IMusicInfo[] = []; //歌曲目录，用数组来存储

  /**
   * @description: 初始化音乐盒
   */
  constructor() {
    this.musicDom = document.createElement("audio");
    this.playIndex = 0;
  }

  /**
   * @description: 事件注册
   * @param {string} evName
   * @param {any} callback
   * @return {*}
   */
  register(evName: string, callback: any) {
    if (evName == "onPause") {
      this.onPause = callback;
    } // 暂停事件
    if (evName == "onPlaying") {
      this.onPlaying = callback;
      this.musicDom.ontimeupdate = () => {
        let progress: number =
          (this.musicDom.currentTime / this.musicDom.duration) * 100;
        if (this.onPlaying) this.onPlaying(progress);
      };
    } // 播放中事件
    if (evName == "onInit") {
      this.onInit = callback;
    }
  }

  /**
   * @description: 数据初始化
   * @param {string} arg
   * @return {*}
   */
  init(arg: string | IMusicInfo[]) {
    if (typeof arg == "string") {
      AxiosIntance.get(arg).then((res) => {
        let json = res.data;
        if (json && json.length > 0) {
          this.addList(json);

          // 触发事件
          if (this.onInit) this.onInit(this.musics);
        }
      });
    } //
    else if (typeof arg == "object") {
      arg.forEach((item) => this.add(item));

      // 触发事件
      if (this.onInit) this.onInit(this.musics);
    }
  }

  /**
   * @description: 添加一首音乐
   * @param {IMusicInfo} info
   * @return {*}
   */
  add(info: IMusicInfo): void {
    this.musics.push(info);
  }

  /**
   * @description: 添加多首音乐
   * @param {IMusicInfo} list
   * @return {*}
   */
  addList(list: IMusicInfo[] | [] | any): void {
    if (!list || typeof list != "object") return;

    for (let i = 0, len = list.length; i < len; i++) {
      const item = list[i] as IMusicInfo;
      this.musics.push(item);
    }
  }

  /**
   * @description: 根据数组下标决定播放哪一首歌
   * @param {number} index
   * @return {*} true 播放中, false 播放失败
   */
  play(index?: number): boolean {
    if (!index) {
      this.musicDom.src = this.musics[this.playIndex].url;
      this.musicDom.play();
    } else if (index >= 0 && index < this.musics.length) {
      this.playIndex = index;
      this.play();
    } else {
      console.trace("索引错误", { index });
    }

    return !this.musicDom.paused;
  }

  /**
   * @description: 暂停音乐
   * @return {boolean} true 暂停成功, false 播放中
   */
  pause(): boolean {
    this.musicDom.pause();
    if (this.onPause) this.onPause();
    return this.musicDom.paused;
  }

  /**
   * @description: 下一首（待编写）
   * @return {void}
   */
  next(): number {
    let total: number = this.musics.length;
    if (this.playIndex == total) this.playIndex = 0;
    else this.playIndex++;
    this.play();
    return this.playIndex;
  }

  /**
   * @description: 上一首（待编写）
   * @return {void}
   */
  prev(): number {
    if (this.playIndex == 0) this.playIndex = this.musics.length - 1;
    else this.playIndex--;
    this.play();
    return this.playIndex;
  }
}
